﻿'use strict';

exports = module.exports;

exports.global = {
    init_log_title: '',
    step_size: 0.01,
    steps: 0,
    nodes: [],
    idx: 0
};

exports.gain = function() {
    let idx = this.global.idx;
    let outIdx = this.global.nodes[idx].inputs[0];
    let yi = (outIdx || outIdx == 0) ? this.global.nodes[outIdx].outvalue : 0;
    let k = this.global.nodes[idx].params[0].value;
    return yi * k;
}

exports.add = function() {
    let idx = this.global.idx;
    let outIdx1 = this.global.nodes[idx].inputs[0];
    let yi1 = (outIdx1 || outIdx1 == 0) ? this.global.nodes[outIdx1].outvalue : 0;
    let outIdx2 = this.global.nodes[idx].inputs[1];
    let yi2 = (outIdx2 || outIdx2 == 0) ? this.global.nodes[outIdx2].outvalue : 0;
    return yi1 + yi2;
}

exports.mult = function() {
    let idx = this.global.idx;
    let outIdx1 = this.global.nodes[idx].inputs[0];
    let yi1 = (outIdx1 || outIdx1 == 0) ? this.global.nodes[outIdx1].outvalue : 0;
    let outIdx2 = this.global.nodes[idx].inputs[1];
    let yi2 = (outIdx2 || outIdx2 == 0) ? this.global.nodes[outIdx2].outvalue : 0;
    return yi1 * yi2;
}

exports.sineWave = function() {
    let idx = this.global.idx;
    let phase = this.global.nodes[idx].params[0].value;
    let freq = this.global.nodes[idx].params[1].value;
    let amp = this.global.nodes[idx].params[2].value;
    let bias = this.global.nodes[idx].params[3].value;
    return amp * Math.sin(freq * this.global.step_size * this.global.steps + phase) + bias;
}

exports.constant = function() {
    let idx = this.global.idx;
    let c = this.global.nodes[idx].params[0].value;
    return c;
}

exports.disp = function() {
    let idx = this.global.idx;
    let outIdx = this.global.nodes[idx].inputs[0];
    let yi = (outIdx || outIdx == 0) ? this.global.nodes[outIdx].outvalue : 0;
    return yi;
}

exports.delay = function() {
    let idx = this.global.idx;
    if (this.global.steps == 0) {
        return this.global.nodes[idx].params[0].value;
    } else {
        let outIdx = this.global.nodes[idx].inputs[0];
        let yi = (outIdx || outIdx == 0) ? this.global.nodes[outIdx].outvalue : 0;
        return yi;
    }
}